
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.Gui;
using Swaf.Security;
using Swaf.BizRule;

using Articles.Gui;

namespace eCom.Gui
{
	public class CreateNewYellowPageEntryWizardBizProc : ArticleManagementBizProc
	{
		public CreateNewYellowPageEntryWizardBizProc(ActiveBizProc procState)
			: base(procState)
		{
			if (State.InProgressAddBizObj == null)
				State.InProgressAddBizObj = createNewBizObj();
		}
		
		protected override void setupRuleNames()
		{
			base.m_updateBizObjRule = "processNewYellowPageEntrySignUp";
		}

		protected override Privileges CurrentEditPrivilege
		{
			get { return Privileges.Edit; }
		}

		protected override object[] getUpdateBizRuleParameters(IBizObj newArticle)
		{
			IBizObj wizardInfo = m_procState.get("info") as IBizObj;
			Debug.Assert(wizardInfo != null);
			return new object[] { newArticle, wizardInfo };
		}

		public override IResults updateBizObj(IBizObj obj)
		{
			IResults rc = null;
			IBizObj wizardInfo = m_procState.get("info") as IBizObj;
			Debug.Assert(wizardInfo != null);

			if (!(bool)wizardInfo.get("stepCompleted") )
			{
				obj.put("productKey", wizardInfo.get("product.partId"));
				rc = base.updateBizObj(obj);
				eCom.SignUpResult results = rc.CallResults as eCom.SignUpResult;
				if (wizardInfo.isNew && rc.ExceptionInfo==null && results != null && results.EComSignUpResultInfo == eComSignUpResult.Success) 
				{
					IBizObj soi = App.bizObjMgr.create("eComSalesOrderItem");
					if(m_procState.get("pid")==null && App.sessionData["CurrentOrderId"]!=null)
						soi.put("orderId", App.sessionData["CurrentOrderId"] as Swaf.BGuid.Guid);
					else
						soi.put("orderId", m_procState.get("pid"));
					soi.put("quantity", 1);
					soi.put("idx", -1);
					soi.put("product", wizardInfo.get("product"));
					rc = App.ruleMgr.execute("updateSalesOrderItem", soi);
					if (rc != null && rc.ExceptionInfo == null)
						wizardInfo.put("stepCompleted", true);
				}
				else if (results != null && results.EComSignUpResultInfo != eComSignUpResult.Success)
				{
					if (results.EComSignUpResultInfo == eComSignUpResult.InvalidNewUserAccount)
						displayMessage("Error-UserIdAlreadyTaken");
					else
						displayMessage("Error-UserSignupGeneralFailure");
				}
			}
			return rc;
		}
		protected bool requiredFieldMissing(string[] fields, IBizObj obj)
		{
			for (int i = 0; i < fields.Length; i++)
				if (Cmn.IsEmpty(obj.get(fields[i])))
				{
					m_procState.BPC.Messenger.add("Error-RequiredFieldMissing", null);
					return true;
				}
			return false;
		}
		protected bool fieldValueIsMatch(object value1, object value2)
		{
			if (string.Compare(value1.ToString(), value2.ToString())!=0)
			{
				m_procState.BPC.Messenger.add("Error-PasswordMisMatch", null);
				return false;
			}
			return true;
		}
	}
}
